/*
 * PromptScreen.java
 *
 * Created on March 14, 2006, 9:51 AM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package Experience.Screens;

import Comm.Util.Logger;
import Experience.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import java.util.Vector;
import java.util.Enumeration;

import com.nokia.mid.ui.*;


/**
 *
 * @author dmgreen
 */

public class ChoiceScreen extends Canvas {
    
    private MIDlet myMIDlet = null;
    protected Font largeFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE);
    protected int largeHeight = largeFont.getHeight();
    
    private int myHeight;
    private int myWidth;
    private int myYPos;
    private String  myLLabel, myRLabel;
    
    private boolean msgValid = false;
    private String myMsg = "";
    
    private int msgLines = 1;
    
    protected Graphics myGfx;
    
    private int msgYPos = 0;
    
    private static final int X_OFFSET = 5;
    
    /** Creates a new instance of PromptScreen */
    public ChoiceScreen(MIDlet mid, String message, int yPos, String leftLabel, String rightLabel) {
        super();
        
        myMsg = message;
        setLeftLabel(leftLabel);
        setRightLabel(rightLabel);
        myYPos = yPos;
        
        myMIDlet = mid;
            
        setFullScreenMode(true);
    
        myWidth = getWidth();
        myHeight = getHeight();
        
        repaint();
        
    }
    
    public int getMsgYPos() {
        return msgYPos;
    }
    
    public void paint(Graphics g) {
        
        myGfx = g;
        
        if (!msgValid) {
            Vector myLines = new Vector();
            // will populate myLines too
            myLines = getLines(myMsg);
            msgValid = true;
        
            int color = myGfx.getColor();
            myGfx.setColor(255,255,255);
            int the_width = myWidth;
            int the_height = ( g.getFont().getHeight() * msgLines  ) ;
            myGfx.fillRect(X_OFFSET, myYPos, the_width, the_height);
            myGfx.setColor(color);
        
            renderLines(X_OFFSET, myYPos, myLines);
        }
        
        
        
        renderLeftLabel();
        renderRightLabel();
        
        
    }
    
    
    protected Vector getLines(String msg) {
        
        Vector words = new Vector();
        
        StringBuffer curWord = new StringBuffer();
        
        char[] data = new char[msg.length()];
        msg.getChars(0, msg.length(), data, 0);
        
        for(int ccnt=0; ccnt < data.length; ccnt++)
        {
            char ch = data[ccnt];
            
            if (ch == ' ') { 
                words.addElement(curWord.toString()); 
                curWord = null;
                curWord = new StringBuffer();
            }
            else { curWord.append(ch); }
        }
        //whatever's left !
        words.addElement(curWord.toString());
        
        
        System.out.println("WORDS");
        for (Enumeration e = words.elements() ; e.hasMoreElements() ;) {
         System.out.println(e.nextElement());
        }
        
        Vector lines = new Vector();
        String line     = "";
        String lineTest = "";
        
        Font curF = myGfx.getFont();
        System.out.println("Cur font is " + curF.toString());
        System.out.println("myWidth is " + myWidth); 
        
        for (int w=0; w<words.size(); w++) {
             
            String nextWord = ((String) words.elementAt(w));
            lineTest = line +  nextWord + " "; 
            System.out.println("linetest is " + lineTest );
            System.out.println ("WIDTH: " + curF.stringWidth(lineTest));
            
            if (curF.stringWidth(lineTest) > myWidth-X_OFFSET) {
                lines.addElement(line);
   
                line = nextWord + " ";
                lineTest = "";
            }
            else {
                line = lineTest;
            }
                
        }
        
        lines.addElement(line);
        return lines;
    }
      
    protected void renderLines(int x, int y, Vector lines) {
        
        int lineoffset = 1;
        int font_height = (myGfx.getFont()).getHeight();
        
        for (Enumeration e = lines.elements() ; e.hasMoreElements() ;) {
         
            String line = (String) e.nextElement();
            myGfx.drawString(line,  x, y + (lineoffset*font_height), Graphics.TOP | Graphics.LEFT);
            
            lineoffset++;
        }
        
        msgYPos = y + (lineoffset*font_height) + font_height;
    }
    
    public void setText(String msg) {
        myMsg = msg;
        msgValid = false;
    }
    
    /*
    private void parseLines() {
        
        myLines = getLines(myMsg);
        msgValid = true;
        
        System.out.println("LINES");
        for (Enumeration e = myLines.elements() ; e.hasMoreElements() ;) {
         System.out.println(e.nextElement());

        }
    }
     */
        
    protected void setLeftLabel(String label) {
        myLLabel = label;
    }
    
    protected void setRightLabel(String label) {
        myRLabel = label;
    }
    
    private void renderLeftLabel(){
        
        int curColor = myGfx.getColor();
        myGfx.setColor(255,255,255);
        
        //20 should actually be the height of a char in the large font !!
        myGfx.fillRect(X_OFFSET, getHeight()-20, getWidth()/2, 20);
        
        myGfx.setColor(curColor);

        Font curFont = myGfx.getFont();
        myGfx.setFont(largeFont);
        myGfx.drawString(myLLabel, X_OFFSET, getHeight(), Graphics.BOTTOM | Graphics.LEFT);
        myGfx.setFont(curFont);
    }
    
    public void renderRightLabel(){
        int curColor = myGfx.getColor();
        myGfx.setColor(255,255,255);
        
        //20 should actually be the height of a char in the large font !!
        myGfx.fillRect(myWidth/2, getHeight()-20, myWidth/2, 20);
        myGfx.setColor(curColor);

        Font curFont = myGfx.getFont();
        myGfx.setFont(largeFont);
        myGfx.drawString(myRLabel, myWidth-X_OFFSET, getHeight(), Graphics.BOTTOM | Graphics.RIGHT);
        myGfx.setFont(curFont);
    }
    
     protected void keyReleased(int keyCode) {
        
        if (keyCode == FullCanvas.KEY_SOFTKEY1) { 
            leftPressed();
        }
        
        else if (keyCode == FullCanvas.KEY_SOFTKEY2) { 
            rightPressed();
        }  
     }
        
     // override these in children classes.
     protected void leftPressed() {}
     protected void rightPressed() {}
     
     public void displayError(String err) {
         Alert msg = new Alert( "Error!",
                       err,
                       null,
                       AlertType.ERROR );
        msg.setTimeout( Alert.FOREVER );
        ((ExpUI) myMIDlet).display.setCurrent(msg);
     }
     
}
